package com.team.cluedo.app.views;

import com.team.cluedo.Game;
import com.team.cluedo.R;
import com.team.cluedo.app.utils.ImageBitmap;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class DrawTouchImageShow extends View{
	   private ImageBitmap[] images = new ImageBitmap[3]; // array that holds the pictures
	   private int imageID = 0; // variable to know what picture is being dragged
	    
	    public DrawTouchImageShow(Context context,ImageBitmap[] imgs) {
	        super(context);
	        images = imgs;
	        setFocusable(true); //necessary for getting the touch events
	    }
	    
	    // the method that draws the balls
	    @Override protected void onDraw(Canvas canvas) {
	        //canvas.drawColor(0xFFCCCCCC);     //if you want another background color       
	        
	    	//draw the balls on the canvas
	    	for (ImageBitmap img : images) {
	            canvas.drawBitmap(img.getBitmap(), img.getX(), img.getY(), null);
	          }

	    }
	    
	    // events when touching the screen
	    public boolean onTouchEvent(MotionEvent event) {
	        int eventaction = event.getAction(); 
	        
	        int X = (int)event.getX(); 
	        int Y = (int)event.getY(); 

	        switch (eventaction ) { 

	        case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
	        	imageID = 0;
	        	for (ImageBitmap img : images) {
	        		// check if inside the bounds of the ball (circle)
	        		// get the center for the ball
	        		int imgX = img.getX();
	        		int imgY = img.getY();
	        		
	        		
	        		// check all the bounds of the image
	        		if (X > img.getX() && X < img.getX()+img.getBitmap().getWidth() && Y > img.getY() && Y < img.getY()+img.getBitmap().getHeight()){
	                	imageID = img.getID();
	                	break;
	                }
	              }
	             
	             break; 


	        case MotionEvent.ACTION_MOVE:   // touch drag with the image
	        	// move the balls the same as the finger
	            if (imageID > 0) {
	            	images[imageID-1].setX(X-25);
	            	images[imageID-1].setY(Y-25);
	            }
	        	
	            break; 

	        case MotionEvent.ACTION_UP: 
	       		// touch drop - just do things here after dropping
	        	Game.cluedo.returnAskedCard(imageID-1);

	            break; 
	        } 
	        // redraw the canvas
	        invalidate(); 
	        return true; 
		
	    }
}
